import cgi
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from chatserver_deepant import UserCollection,DeepantHandler;
import urlparse
from os import curdir, sep, path;

try:
    from urlparse import parse_qs, parse_qsl
except ImportError:
    # fall back for Python 2.5 
    from cgi import parse_qs, parse_qsl
    
class PythonHandler(DeepantHandler, BaseHTTPRequestHandler):
    def do_GET(self):
        return DeepantHandler.handlerGet(self);
    def do_POST(self):
        return DeepantHandler.handlerPost(self);
        
    def writeBody(self, data):
        self.wfile.write(data);
    
    def writeHeader(self, name, value):
        self.send_header(name, value)
        
    def endWriteHeader(self):
        self.end_headers();
        
    def writeResponseStatus(self, status):
        self.send_response(status)
        
    def getQuery(self):
        parsed_path = urlparse.urlparse(self.path);
        real_path = parsed_path.path;
        query = parsed_path.query;
        return query
        
    def getRealPath(self):
        parsed_path = urlparse.urlparse(self.path);
        real_path = parsed_path.path;
        return  real_path      
        
    def getRequestHeader(self, name):
        return self.headers.getheader(name)
    
    def getRequestBody(self):
        content_len = int(self.getRequestHeader('content-length'))
        post_body = self.rfile.read(content_len)
        return post_body
        
    def Log(self, msg):
        print msg
        pass;        
  
def main():
    try:
        server = HTTPServer(('', 8989), PythonHandler)
        print 'started httpserver...'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down '
        server.socket.close()

if __name__ == '__main__':
    main()